package StringProject;

import javax.print.DocFlavor;
import java.util.Date;
import java.util.Scanner;

public class StringTestDemo {
    public static void main(String str[]) {
        /*
        String str1 = new String("good");
        System.out.println(str1);
        char a[] = {'g','o','o','d'};
        String str2 = new String(a);
        System.out.println(str2);

        char b[] = {'g','o','o','d'};
        String str3 = new String(a,1,2);
        System.out.println(str3);

       //连接字符串
        String str1 = new String("hello");
        String str2 = new String("world");
        String str3 = str1 + " " +str2;
        System.out.println(str3);
         */
        //获取字符串长度
        String str4 = new String("we are students we are students");
        int size = str4.length();
        System.out.println(size);
        int size2 = str4.indexOf("ar");
        System.out.println(size2);
        int size3 = str4.lastIndexOf("");
        System.out.println(size3);
        char mychar = str4.charAt(4);
        System.out.println(mychar);

        //截取字符串
        String str5 = str4.substring(3);
        System.out.println("str5: " + str5);
        String str6 = str4.substring(3, 6);
        System.out.println("str6: " + str6);

        //去除空格
        System.out.println("字符串原来的长度：" + str4.length());
        System.out.println("字符串现在的长度：" + str4.trim().length());

        //字符串替换
        String newstr = str4.replace("we", "WE");
        System.out.println("newstr: " + newstr);

        //判断字符串的开始和结尾
        boolean b1 = str4.startsWith("we");
        boolean b2 = str4.startsWith("WE");
        boolean b3 = str4.endsWith("TS");
        boolean b4 = str4.endsWith("students");
        System.out.println("字符串str4是以'we'开始的吗 " + b1);
        System.out.println("字符串str4是以'WE'开始的吗 " + b2);
        System.out.println("字符串str4是以's'结束的吗 " + b3);
        System.out.println("字符串str4是以'students'结束的吗 " + b4);

        //比较字符串
        String str7 = new String("abc");
        String str8 = new String("ABC");
        boolean b5 = str7.equals(str8);
        boolean b6 = str7.equalsIgnoreCase(str8);
        System.out.println(str7 + " equals " + str8 + " :" + b5);
        System.out.println(str7 + " qualsIgnoreCase " + str8 + " :" + b6);

        //大小写转换
        String newstr2 = str7.toUpperCase();
        String newstr3 = str8.toLowerCase();
        System.out.println(newstr2);
        System.out.println(newstr3);

        //字符串分割
        String str9 = "192.168.0.1";
        String[] firstArray = str9.split("\\.");
        String[] secondArray = str9.split("\\.", 2);
        System.out.println("str9的原值为：[ " + str9 + " ]");
        System.out.println("全部分割的结果： ");
        for (String a : firstArray) {
            System.out.println("[" + a + "]");
        }
        System.out.println();
        System.out.println("分割两次的结果： ");
        for (String a : secondArray) {
            System.out.println("[" + a + "]");
        }
        System.out.println();

        //日期和时间字符串格式化
        Date date = new Date();
        String year = String.format("%tY", date);   //将date进行格式化
        String month = String.format("%tb", date);
        String day = String.format("%td", date);
        String hour = String.format("%tH", date);
        String minute = String.format("%tM", date);
        String second = String.format("%tS", date);
        System.out.println("今年是：" + year + "年");
        System.out.println("现在是：" + month);
        System.out.println("今天是：" + day + "号");
        System.out.println("现在是：" + hour + "时" + minute + "分" + second + "秒");

        //组合日期格式
        String time = String .format("%tc", date);
        String form1 = String.format("%tF", date);
        String form2 = String.format("%tD", date);
        String form3 = String.format("%tr", date);
        String form4 = String.format("%tT", date);
        String form5 = String.format("%tR", date);
        System.out.println("全部日期和时间信息： " + time);
        System.out.println("年-月-日 格式：" + form1);
        System.out.println("月/日/年 格式：" + form2);
        System.out.println("时：分：秒 PM(AM) 格式（12小时制）：" + form3);
        System.out.println("时：分：秒 格式（24小时制）：" + form4);
        System.out.println("时：分 格式（24时制）：" + form5);

        //正则表达式
       /* 设计思路：
        [邮箱名]@[服务器名].[中间名].[后缀]
        合法字符串@合法字符串.字符串(可无).字符串（最大三个字符）
        \\w    @\\w      \\.* \\w{2,3}*\\.\\w{2,3}*/
       String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
       String email1 = "121232";
       String email2 = "123@qwe.com";
       String email3 = "@163.vip.com";
       String email4 = "suohu@163.vip.com";

       if(email1.matches(regex)){
            System.out.println(email1 + "是一个合法的Email地址");
        }
        if(email2.matches(regex)){
            System.out.println(email2 + "是一个合法的Email地址");
        }
        if(email3.matches(regex)){
            System.out.println(email3 + "是一个合法的Email地址");
        }
        if(email4.matches(regex)){
            System.out.println(email4 + "是一个合法的Email地址");
        }
    }
}
